package com.hlkj.util;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

	/**
	 * 将数字金额转换为中文大写
	 * 
	 * @author zhukui
	 * @param Money
	 * @return
	 */
	public static String changeMoney(Double Money) {
		try {
			if (Money == null) {
				return "零元";
			}
			String result = "";// 大写金额
			String strValue = "";// 小写金额
			boolean ifNeg = false;
			if (Money.doubleValue() < 0) {
				DecimalFormat df = new DecimalFormat("####.00");
				strValue = df.format(Money.doubleValue() * -1);
				ifNeg = true;
			} else {
				DecimalFormat df = new DecimalFormat("####.00");
				strValue = df.format(Money);
			}
			int dotIndex = strValue.indexOf('.');
			String afterDot;
			if (dotIndex == -1) {
				dotIndex = strValue.length();
				afterDot = "";
			} else
				afterDot = strValue.substring(dotIndex + 1);

			String beforeDot = strValue.substring(0, dotIndex).trim();
			int len = 0;
			len = beforeDot.length();
			char[] unit = { '圆', '万', '亿', '万' };
			char[] subunit = { '拾', '佰', '仟' };
			int i = 0, j = 0, k = 0; // j is use to subunit,k is use to unit
			char oldC = '3';
			char zero = '零';
			char cc = '0';
			result = unit[0] + result;
			boolean oldHasN = false;
			boolean hasN = false;
			boolean allZero = true;
			for (i = 0; i < len; i++) {
				if (j == 0 && i != 0) {
					if (!hasN) {
						if ((k % 2) == 0) {
							result = result.substring(1);
						}
					} else {
						if (oldC == '0')
							result = zero + result;
					}
					result = unit[k] + result;
					oldHasN = hasN;
					hasN = false;
				}

				cc = beforeDot.charAt(len - i - 1);
				if (oldC == '0' && cc != oldC) {
					if (hasN)
						result = zero + result;
				}
				if (cc != '0') {
					if (j != 0)
						result = subunit[j - 1] + result;
					char dig = '0';
					switch (cc) {
					case '1':
						dig = '壹';
						break;
					case '2':
						dig = '贰';
						break;
					case '3':
						dig = '叁';
						break;
					case '4':
						dig = '肆';
						break;
					case '5':
						dig = '伍';
						break;
					case '6':
						dig = '陆';
						break;
					case '7':
						dig = '柒';
						break;
					case '8':
						dig = '捌';
						break;
					case '9':
						dig = '玖';
						break;
					}
					if (dig == '0')
						return "err";
					hasN = true;
					allZero = false;
					result = dig + result;
				}
				oldC = cc;
				j++;
				if (j == 4) {
					k++;
					j = 0;
				}
			}
			if (allZero) {
				result = "零圆";
			} else {
				int bb = 0;
				if (!hasN) {
					bb++;
					if (!oldHasN) {
						bb++;
					}
				}
				if (bb != 0)
					result = result.substring(bb);
				if (result.charAt(0) == '零')
					result = result.substring(1);
			}

			len = 0;
			len = afterDot.length();
			char[] unit1 = { '角', '分' };
			hasN = false;
			boolean zoo = false;
			if (len > 2)
				len = 2;
			if (len != 0) {
				for (i = 0; i < len; i++) {
					cc = afterDot.charAt(i);
					char dig = 'A';
					switch (cc) {
					case '1':
						dig = '壹';
						break;
					case '2':
						dig = '贰';
						break;
					case '3':
						dig = '叁';
						break;
					case '4':
						dig = '肆';
						break;
					case '5':
						dig = '伍';
						break;
					case '6':
						dig = '陆';
						break;
					case '7':
						dig = '柒';
						break;
					case '8':
						dig = '捌';
						break;
					case '9':
						dig = '玖';
						break;
					case '0':
						dig = '0';
						break;
					}
					if (dig == 'A')
						return "err";
					if (i == 0 && dig == '0') {
						zoo = true;
					}
					if (dig != '0') {
						hasN = true;
						if (i == 1 && zoo) {
							result = result + "零";
						}
						result += dig;
						result += unit1[i];
					}
				}

				if (!hasN)
					result += '整';
			}

			else {
				result += '整';
			}

			if (ifNeg) {
				result = "负" + result;
			}
			if (result.indexOf("分") == -1 && result.indexOf("整") == -1) {
				result = result + "整";
			}
			return result;
		} catch (Exception e) {
			return ("err:" + e);
		}
	}

	/**
	 * 获取当前时间
	 * 
	 */
	public static String getTime() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
	}

	/**
	 * 清除掉所有特殊字符
	 * 
	 * @param str
	 * @return
	 */
	public static String StringFilter(String str) {
		// 只允许字母和数字
		// String regEx = "[^a-zA-Z0-9]";
		// 清除掉所有特殊字符
		String nstr = "";
		try {
			if (!str.equals("")) {
				String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/\\?~！@#￥%……& amp;*（）——+|{}【】‘；：”“’。，、？]";
				Pattern p = Pattern.compile(regEx);
				Matcher m = p.matcher(str);
				nstr = m.replaceAll("").trim();
				nstr = nstr.replace("\\", "");
				return nstr;
			} else {
				return nstr;
			}
		} catch (Exception e) {
			return nstr;
		}
	}

	/***
	 * 去掉HTML标记
	 * 
	 * @param str
	 * @return
	 */
	public static String removeHtml(String str) {
		String result = "";
		if (str != null && !"".equals(str)) {
			Pattern pattern = Pattern.compile("<.+?>", Pattern.DOTALL);
			Matcher matcher = pattern.matcher(str);
			result = matcher.replaceAll("");
		}
		return result;

	}

	/***
	 * 获取1-8位随机数字
	 */
	public static String getRandomNumber(int number) {
		String strInt = " ";
		Integer i = new Integer((int) (Math.random() * 10000000));
		strInt = String.valueOf(i);
		// System.out.println("Integer_max="+Integer.MAX_VALUE);
		// System.out.println("Integer_min="+Integer.MIN_VALUE);
		if (number >= 1 && strInt.length() != number) {
			return getRandomNumber(number);
		} else {
			return strInt;
		}
	}

	public static void main(String[] args) {
		System.out.println(changeMoney(100000000000.00));
	}
}
